/**
 * Copyright 2008 Mathias Doenitz, http://lis.to/
 *
 * This file is part of the lis.to java desktop client. The lis.to java desktop client is free software: you can
 * redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option) any later version.
 *
 * The lis.to java desktop client is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with the lis.to java desktop client.
 * If not, see http://www.gnu.org/licenses/
 */

package listo.client.taskfilters;

import com.google.inject.Inject;
import com.google.inject.Singleton;
import listo.client.model.Task;
import listo.client.model.Tasks;
import listo.client.tasklists.TaskList;
import listo.client.tasklists.TaskListManager;
import listo.utils.types.Cal;
import listo.utils.types.DateTime;

import java.util.Calendar;

@Singleton
public class TaskListFilter extends BaseFilter implements TaskFilter.Listener, TaskListManager.ActiveTaskListListener {

    public static class Settings {

        // the filter range in days relative to today, can be null to indicate "no boundary"
        public final Integer startDay;
        public final Integer endDay;

        public Settings(Integer startDay, Integer endDay) {
            this.startDay = startDay;
            this.endDay = endDay;
        }
    }

    private Tasks unfilteredTasks;
    private Settings settings;

    @Inject
    public TaskListFilter(SearchFilter searchFilter, TaskListManager taskListManager) {
        searchFilter.addFilterListener(this);
        taskListManager.addActiveTaskListListener(this);
    }

    public void tasksChanged(Object updateObject) {
        unfilteredTasks = (Tasks) updateObject;
        if (settings != null) update();
    }

    public void activeTaskListChanged(TaskList activeTaskList, Settings activeFilterSettings) {
        settings = activeFilterSettings;
        if (unfilteredTasks != null) update();
    }

    public void update() {
        Tasks filteredTasks = new Tasks();
        for (Task task : unfilteredTasks) {
            if (passes(task)) {
                filteredTasks.add(task);
            }
        }
        notifyListeners(filteredTasks);
    }

    public boolean passes(Task task) {
        if (settings.startDay == null && settings.endDay == null) return true;

        DateTime rangeStart = null;
        DateTime rangeEnd = null;
        if (settings.startDay != null) {
            rangeStart = new Cal().beginningOfDay().add(Calendar.DATE, settings.startDay).getDateTime();
        }
        if (settings.endDay != null) {
            rangeEnd = new Cal().endOfDay().add(Calendar.DATE, settings.endDay).getDateTime();
        }

        DateTime due = task.getDue();
        return due != null &&
                (rangeStart == null || !rangeStart.after(due)) &&
                (rangeEnd == null || rangeEnd.after(due));
    }

}